package com.oros.db;
import java.io.Serializable;
public class AETableDefinition implements Serializable,Cloneable {
	 /**
	 * 
	 */
	private static final long serialVersionUID = 3605640471940448317L;
	public String tableName;
     public String[] columnNames;
     public Object[] columnDefaults;
     public Class<?>[] columnTypes;
     public String primaryKeyColumn,parentColumn;
 	 public AEGeneratedData[] generatedData;
     public AETableDefinition()
     {
    	 parentColumn = null;
    	 generatedData=null;
     }
     public AETableDefinition(String tableName,String[] columnNames,
    		   Object[] columnDefaults,Class<?>[] columnTypes,
    		   String primaryKeyColumn,String parentColumn)
     {
    	 this.tableName = tableName;
    	 this.columnNames = columnNames;
    	 this.columnDefaults = columnDefaults;
    	 this.columnTypes = columnTypes;
    	 this.primaryKeyColumn = primaryKeyColumn;
    	 this.parentColumn = parentColumn;
    	 this.generatedData = null;
    	 int n = columnTypes.length;
    	 for(int i=0;i<n;i++)
    	 {
    		 Object o = columnDefaults[i];
    		 Class<?> tp = columnTypes[i];
    		 if (o!=null)
    		 {
    			 if (o.getClass()!=tp)
    			 {
    				 if (tp==Long.class)
    				 {
    					 if (o instanceof Integer)
    					 {
    						 columnDefaults[i]=new Long(((Integer)o).longValue());
    					 }
    					 else 
    					try
    					 {
    						columnDefaults[i]=new Long(Long.parseLong(o.toString()));
    					 }
    					 catch(Exception e)
    					 {
    						 columnDefaults[i]=new Long(0L);
    					 }
    				 }
    				 else if (tp==Integer.class)
    				 {
    					 if (o instanceof Long)
    					 {
    						 columnDefaults[i]=new Integer(((Long)o).intValue());
    					 }
    					 else 
    					try
    					 {
    						columnDefaults[i]=new Integer(Integer.parseInt(o.toString()));
    					 }
    					 catch(Exception e)
    					 {
    						 columnDefaults[i]=new Integer(0);
    					 }
    				 }
    				 else if (tp==String.class)
    				 {
    					 columnDefaults[i]=o.toString();
    				 }
    			 }
    		 }
    	 }
     }
     public void setGeneratedData(AEGeneratedData[] generatedData)
     {
    	 this.generatedData = generatedData;
     }
     public void addGeneratedColumns(AEGeneratedColumn[] generatedColumns)
     {
    	 if (generatedColumns==null || generatedColumns.length==0) return;
    	 int n= generatedColumns.length;
    	 int oldColAmnt = columnNames.length;
    	 int newColAmnt = oldColAmnt + n;
    	 int o = generatedData==null?0:generatedData.length;
    	 String[] tmpColumnNames = new String[newColAmnt];
    	 Object[] tmpColumnDefaults = new Object[newColAmnt];
    	 Class<?>[] tmpColumnTypes = new Class<?>[newColAmnt];
    	 AEGeneratedData[] tmpGeneratedData = new AEGeneratedData[o+n];
    	 for(int i=0;i<oldColAmnt;i++)
    	 {
    		 tmpColumnNames[i]=columnNames[i];
    		 tmpColumnDefaults[i]=columnDefaults[i];
    		 tmpColumnTypes[i]=columnTypes[i];
    	 }
    	 for(int i=0;i<o;i++)
    	 {
    		 tmpGeneratedData[i]=generatedData[i];
    	 }
    	 for(int i=0;i<n;i++)
    	 {
    		 tmpColumnNames[oldColAmnt+i]=generatedColumns[i].columnName;
    		 tmpColumnDefaults[oldColAmnt+i]=generatedColumns[i].defaultValue;
    		 tmpColumnTypes[oldColAmnt+i]=generatedColumns[i].type;
    		 tmpGeneratedData[i+o]=generatedColumns[i].generator;
    	 }
    	 columnNames = tmpColumnNames;
    	 columnDefaults = tmpColumnDefaults;
    	 columnTypes = tmpColumnTypes;
    	 generatedData = tmpGeneratedData;
     }
     @Override
     public Object clone() 
     {
         try {
             AETableDefinition o=(AETableDefinition)super.clone();
             //it is not necessary to clone strings since they are unique;
             o.columnNames = columnNames.clone();
             o.columnDefaults = columnDefaults.clone();
             o.columnTypes = columnTypes.clone();
             if (generatedData!=null)
             {
            	 o.generatedData = generatedData.clone();
             }
             return o;
         } catch(CloneNotSupportedException e) {
             //it is impossible
             throw new InternalError(e.toString());
         }
     }
}
